//
//  SZNewsWidthImgCell.m
//  03-仿网易新闻
//
//  Created by ItHeiMa on 2017/1/3.
//  Copyright © 2017年 itHeima. All rights reserved.
//

#import "SZNewsWidthImgCell.h"
#import "UIImageView+AFNetworking.h"
#import "Masonry.h"

#import <objc/runtime.h>
@implementation SZNewsWidthImgCell

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    
    return self;
}

- (void)setModel:(SZNewsModel *)model {
    _model = model;
    //设置图片
    NSURL *url = [NSURL URLWithString: _model.imgsrc];
    UIImage *placeImg = [UIImage imageNamed:@"about_hd"];
    [_iconImageView setImageWithURL:url placeholderImage:placeImg];
    
    //设置label
    _newsTitle.text = _model.title;
    
    //设置source
    _source.text = _model.source;
    
    //设置回贴
    _replyCount.text = @(_model.replyCount).description;
}

- (void) setupUI {
    //1. 创建并添加子视图
    _iconImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_iconImageView];
    
    _newsTitle = [[UILabel alloc] init];
    _newsTitle.font = [UIFont systemFontOfSize:18];
    _newsTitle.textColor = [UIColor blackColor];
    _newsTitle.numberOfLines = 0;
    [self.contentView addSubview:_newsTitle];
    
    _source = [[UILabel alloc] init];
    _source.font = [UIFont systemFontOfSize:14];
    _source.textColor = [UIColor lightGrayColor];
    _source.numberOfLines = 0;
    [self.contentView addSubview:_source];
    
    _replyCount = [[UILabel alloc] init];
    _replyCount.font = [UIFont systemFontOfSize:14];
    _replyCount.textColor = [UIColor lightGrayColor];
    _replyCount.numberOfLines = 0;
    [self.contentView addSubview:_replyCount];
    
    //2. 自动布局
    [_newsTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
    }];
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_newsTitle.mas_bottom).offset(10);
        make.left.equalTo(_newsTitle);
        make.right.equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo(100);
    }];
    
    [_source mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconImageView.mas_bottom).offset(10);
        make.bottom.equalTo(self.contentView).offset(-10);
        make.left.equalTo(_iconImageView);
    }];
    
    [_replyCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_source);
        make.bottom.equalTo(_source);
        make.right.equalTo(_iconImageView);
    }];
}

@end
